package com.teach.controller.photo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.teach.R;
import com.teach.dto.LoginUser;
import com.teach.dto.PhotoFormDTO;
import com.teach.dto.PhotoPicSearch;
import com.teach.entity.PhotoPic;
import com.teach.service.photo.PhotoPicService;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/photo")
@Slf4j
public class PhotoPicController {

	@Autowired
	private PhotoPicService impl;
	
	@RequestMapping("/picAll.do")
	public Object doPicAll(@RequestParam("index")int index,
			@RequestParam("len")int len) {
		return impl.selectAll(index,len);
	}
	
	@RequestMapping("/greatDESC.do")
	public Object doGreatDESC() {
		return impl.selectGreatDESC();
	}

	/*
	 * @RequestMapping("/test2.do") public Object doPic2() { return
	 * impl.selectByPrimaryKey(1l); }
	 */
	@RequestMapping("/picDetails.do")
	public Object doPicDetails(
			@RequestParam("title")String title,
			@RequestParam("writerId")String writerId,
			@RequestParam("picClassId")Long picClassId) {
		return impl.selectPicDetails(title,writerId,picClassId);
	}
	@RequestMapping("/picDetailsDESC.do")
	public Object doPicDetailsDESC(
			@RequestParam("title")String title,
			@RequestParam("writerId")String writerId,
			@RequestParam("picClassId")Long picClassId) {
		return impl.selectPicDetailsDESC(title,writerId,picClassId);
	}
	
	@PostMapping("/upload-pic.do")
	public R doUpload(LoginUser user, PhotoFormDTO form,MultipartFile img){
		log.debug("LoginUser : {}", user);
		log.debug("PhotoFormDTO : {}", form);
		log.debug("MultipartFile : {}", img);
		impl.insertSelective(user, form, img);
		return R.ok();
	}
	
	@RequestMapping("/picture.do")
	public Object dopicture(PhotoPicSearch search) {
		log.debug("PhotoPicSearch : {}", search);
		PageInfo<PhotoPic> pageSearch = impl.pageSearch(search);
		log.debug("PageInfo<PhotoPic> : {}", pageSearch);
		return pageSearch;
	}
	
	@RequestMapping("/uploade-status.do")
	public Object doUpdateStatus(@RequestParam(required = true)Long photoPicId, @RequestParam(required = true)String status) {
		log.debug("作品id Long photoPicId : {}", photoPicId);
		log.debug("作品修改状态 String status : {}", status);
		PhotoPic record = new PhotoPic();
		record.setPhotoPicId(photoPicId);
		record.setPhotoPicStatus(status);
		int affected = impl.updateByPrimaryKeySelective(record);
		log.debug("修改受影响行数 affected : {}", affected);
		return R.ok();
	}
}
